package Model;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DBAccess.DBAccess;

@WebServlet("/searchusers")
public class SearchServlet extends HttpServlet  {
//select a.userid from accountdb as a inner join personaldb as p on a.userid=p.userid where age>20
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		new DBAccess();
		String name = request.getParameter("name");
		name = name==""||name==null?"%":"%"+name+"%";
		String agel = request.getParameter("agelower");
		agel = agel==""||agel==null?"0":agel;
		String ageu = request.getParameter("ageupper");
		ageu = ageu==""||ageu==null?"999":ageu;
		String[] interests = request.getParameterValues("interests");
	 	String orientation = request.getParameter("orientation");
	 	orientation = orientation==""||orientation==null?"%":orientation;
	 	String gender = request.getParameter("gender");
	 	String religion = request.getParameter("religion");
	 	religion = religion==""||religion==null?"%":religion;
	 	String ethnicity = request.getParameter("ethnicity");
	 	ethnicity = ethnicity==""||ethnicity==null?"%":ethnicity;
	 	String interestsString = "";
	 	if (interests != null){
	 		StringBuffer buf = new StringBuffer();
		 	for (int i=0; i<interests.length; i++){
		 		buf.append(interests[i]+";");
		 	}
		 	interestsString = buf.toString();
	 	}
	 	interestsString = interestsString.equals("")?"%":interestsString;
	 	PreparedStatement ps = null;
	 	ResultSet rs = null;
	 	try{
	 		String stmt ="SELECT a.userid FROM accountdb AS a INNER JOIN personaldb AS p ON a.userid=p.userid WHERE"
	 				+" name LIKE ? AND interests LIKE ? AND orientation LIKE ? AND sex LIKE ? AND religion LIKE ? AND ethnicity LIKE ? " +
	 				"AND age >= ? AND age <= ?";
	 		ps = DBAccess.con.prepareStatement(stmt);
	 		ps.setString(1, name);
	 		ps.setString(2, interestsString);
	 		ps.setString(3, orientation);
	 		ps.setString(4, gender);
	 		ps.setString(5, religion);
	 		ps.setString(6, ethnicity);
	 		ps.setString(7, agel);
	 		ps.setString(8, ageu);
	 		System.out.println(ps.toString());
	 		rs = ps.executeQuery();
	 		ArrayList<Integer> resultsList = new ArrayList<Integer>();
	 		for(int i=0; i<100 && rs.next(); i++){
	 			resultsList.add(rs.getInt(1));
	 		}
//	 		Object results = (Object)resultsList.toArray();
//	 		request.setAttribute("results", results);
			request.getSession().setAttribute("results",resultsList);
			request.getSession().setAttribute("searched", Boolean.TRUE);
	 		
	 	}catch(Exception e){
	 		e.printStackTrace();
	 	}
	 	DBAccess.close(ps, rs);
	 	response.sendRedirect("searchresults.jsp");
		return;
	}
}
